<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="pickle-protocol.html" />
<link rel="prev" href="node316.html" />
<link rel="parent" href="module-pickle.html" />
<link rel="next" href="pickle-protocol.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>13.1.4 What can be pickled and unpickled?</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.3 Usage"
  href="node316.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1 pickle  "
  href="module-pickle.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.5 the pickle protocol"
  href="pickle-protocol.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node316.html">13.1.3 Usage</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-pickle.html">13.1 pickle  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pickle-protocol.html">13.1.5 The pickle protocol</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0015140000000000000000">
13.1.4 What can be pickled and unpickled?</a>
</h2>

<p>
The following types can be pickled:

<p>

<ul>
<li><code>None</code>, <code>True</code>, and <code>False</code>

<p>
</li>
<li>integers, long integers, floating point numbers, complex numbers

<p>
</li>
<li>normal and Unicode strings

<p>
</li>
<li>tuples, lists, sets, and dictionaries containing only picklable objects

<p>
</li>
<li>functions defined at the top level of a module

<p>
</li>
<li>built-in functions defined at the top level of a module

<p>
</li>
<li>classes that are defined at the top level of a module

<p>
</li>
<li>instances of such classes whose <tt class="member">__dict__</tt> or
<tt class="method">__setstate__()</tt> is picklable  (see
section&nbsp;<a href="pickle-protocol.html#pickle-protocol">13.1.5</a> for details)

<p>
</li>
</ul>

<p>
Attempts to pickle unpicklable objects will raise the
<tt class="exception">PicklingError</tt> exception; when this happens, an unspecified
number of bytes may have already been written to the underlying file.
Trying to pickle a highly recursive data structure may exceed the
maximum recursion depth, a <tt class="exception">RuntimeError</tt> will be raised
in this case. You can carefully raise this limit with 
<tt class="function">sys.setrecursionlimit()</tt>.

<p>
Note that functions (built-in and user-defined) are pickled by ``fully
qualified'' name reference, not by value.  This means that only the
function name is pickled, along with the name of module the function
is defined in.  Neither the function's code, nor any of its function
attributes are pickled.  Thus the defining module must be importable
in the unpickling environment, and the module must contain the named
object, otherwise an exception will be raised.<a name="tex2html112"
  href="#foot36741"><sup>13.4</sup></a>
<p>
Similarly, classes are pickled by named reference, so the same
restrictions in the unpickling environment apply.  Note that none of
the class's code or data is pickled, so in the following example the
class attribute <code>attr</code> is not restored in the unpickling
environment:

<p>
<div class="verbatim"><pre>
class Foo:
    attr = 'a class attr'

picklestring = pickle.dumps(Foo)
</pre></div>

<p>
These restrictions are why picklable functions and classes must be
defined in the top level of a module.

<p>
Similarly, when class instances are pickled, their class's code and
data are not pickled along with them.  Only the instance data are
pickled.  This is done on purpose, so you can fix bugs in a class or
add methods to the class and still load objects that were created with
an earlier version of the class.  If you plan to have long-lived
objects that will see many versions of a class, it may be worthwhile
to put a version number in the objects so that suitable conversions
can be made by the class's <tt class="method">__setstate__()</tt> method.

<p>
<br><hr><h4>Footnotes</h4>
<dl>
<dt><a name="foot36741">... raised.</a><A
 HREF="node317.html#tex2html112"><sup>13.4</sup></a></dt>
<dd>The exception
raised will likely be an <tt class="exception">ImportError</tt> or an
<tt class="exception">AttributeError</tt> but it could be something else.

</dd>
</dl>
<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.3 Usage"
  href="node316.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="13.1 pickle  "
  href="module-pickle.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="13.1.5 the pickle protocol"
  href="pickle-protocol.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node316.html">13.1.3 Usage</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-pickle.html">13.1 pickle  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="pickle-protocol.html">13.1.5 The pickle protocol</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
